home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / SOUND / MP3CONV / !MP3Conv / c / portableio < prev    next >
Text File  |  1997-02-17  |  7KB  |  357 lines

  1. /* Copyright (C) 1988-1991 Apple Computer, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * Warranty Information
  5.  * Even though Apple has reviewed this software, Apple makes no warranty
  6.  * or representation, either express or implied, with respect to this
  7.  * software, its quality, accuracy, merchantability, or fitness for a 
  8.  * particular purpose.  As a result, this software is provided "as is,"
  9.  * and you, its user, are assuming the entire risk as to its quality
  10.  * and accuracy.
  11.  *
  12.  * This code may be used and freely distributed as long as it includes
  13.  * this copyright notice and the warranty information.
  14.  *
  15.  *
  16.  * Motorola processors (Macintosh, Sun, Sparc, MIPS, etc)
  17.  * pack bytes from high to low (they are big-endian).
  18.  * Use the HighLow routines to match the native format
  19.  * of these machines.
  20.  *
  21.  * Intel-like machines (PCs, Sequent)
  22.  * pack bytes from low to high (the are little-endian).
  23.  * Use the LowHigh routines to match the native format
  24.  * of these machines.
  25.  *
  26.  * These routines have been tested on the following machines:
  27.  *    Apple Macintosh, MPW 3.1 C compiler
  28.  *    Apple Macintosh, THINK C compiler
  29.  *    Silicon Graphics IRIS, MIPS compiler
  30.  *    Cray X/MP and Y/MP
  31.  *    Digital Equipment VAX
  32.  *
  33.  *
  34.  * Implemented by Malcolm Slaney and Ken Turkowski.
  35.  *
  36.  * Malcolm Slaney contributions during 1988-1990 include big- and little-
  37.  * endian file I/O, conversion to and from Motorola's extended 80-bit
  38.  * floating-point format, and conversions to and from IEEE single-
  39.  * precision floating-point format.
  40.  *
  41.  * In 1991, Ken Turkowski implemented the conversions to and from
  42.  * IEEE double-precision format, added more precision to the extended
  43.  * conversions, and accommodated conversions involving +/- infinity,
  44.  * NaN's, and denormalized numbers.
  45.  *
  46.  * $Id: portableio.c,v 2.6 1991/04/30 17:06:02 malcolm Exp $
  47.  *
  48.  * $Log: portableio.c,v $
  49.  * Revision 2.6  91/04/30  17:06:02  malcolm
  50.  *
  51.  * 12/16/96 Johan Hagman    Commented out code not necessary
  52.  *                for mpeg3play 0.9 (with #if 0).
  53.  */
  54.  
  55. #include    <stdio.h>
  56. #include    <math.h>
  57. #include    "portableio.h"
  58.  
  59. /****************************************************************
  60.  * Big/little-endian independent I/O routines.
  61.  ****************************************************************/
  62.  
  63. int
  64. ReadByte(FILE *fp)
  65. {
  66.     int    result;
  67.  
  68.     result = getc(fp) & 0xff;
  69.     if (result & 0x80)
  70.         result = result - 0x100;
  71.     return result;
  72. }
  73.  
  74.  
  75. int
  76. Read16BitsLowHigh(FILE *fp)
  77. {
  78.     int    first, second, result;
  79.  
  80.     first = 0xff & getc(fp);
  81.     second = 0xff & getc(fp);
  82.  
  83.     result = (second << 8) + first;
  84. #ifndef    THINK_C42
  85.     if (result & 0x8000)
  86.         result = result - 0x10000;
  87. #endif    /* THINK_C */
  88.     return(result);
  89. }
  90.  
  91.  
  92. int
  93. Read16BitsHighLow(FILE *fp)
  94. {
  95.     int    first, second, result;
  96.  
  97.     first = 0xff & getc(fp);
  98.     second = 0xff & getc(fp);
  99.  
  100.     result = (first << 8) + second;
  101. #ifndef    THINK_C42
  102.     if (result & 0x8000)
  103.         result = result - 0x10000;
  104. #endif    /* THINK_C */
  105.     return(result);
  106. }
  107.  
  108.  
  109. void
  110. Write8Bits(FILE *fp, int i)
  111. {
  112.     putc(i&0xff,fp);
  113. }
  114.  
  115.  
  116. void
  117. Write16BitsLowHigh(FILE *fp, int i)
  118. {
  119.     putc(i&0xff,fp);
  120.     putc((i>>8)&0xff,fp);
  121. }
  122.  
  123.  
  124. void
  125. Write16BitsHighLow(FILE *fp, int i)
  126. {
  127.     putc((i>>8)&0xff,fp);
  128.     putc(i&0xff,fp);
  129. }
  130.  
  131.  
  132. int
  133. Read24BitsHighLow(FILE *fp)
  134. {
  135.     int    first, second, third;
  136.     int    result;
  137.  
  138.     first = 0xff & getc(fp);
  139.     second = 0xff & getc(fp);
  140.     third = 0xff & getc(fp);
  141.  
  142.     result = (first << 16) + (second << 8) + third;
  143.     if (result & 0x800000)
  144.         result = result - 0x1000000;
  145.     return(result);
  146. }
  147.  
  148. #define    Read32BitsLowHigh(f)    Read32Bits(f)
  149.  
  150.  
  151. int
  152. Read32Bits(FILE *fp)
  153. {
  154.     int    first, second, result;
  155.  
  156.     first = 0xffff & Read16BitsLowHigh(fp);
  157.     second = 0xffff & Read16BitsLowHigh(fp);
  158.  
  159.     result = (second << 16) + first;
  160. #ifdef    CRAY
  161.     if (result & 0x80000000)
  162.         result = result - 0x100000000;
  163. #endif    /* CRAY */
  164.     return(result);
  165. }
  166.  
  167.  
  168. int
  169. Read32BitsHighLow(FILE *fp)
  170. {
  171.     int    first, second, result;
  172.  
  173.     first = 0xffff & Read16BitsHighLow(fp);
  174.     second = 0xffff & Read16BitsHighLow(fp);
  175.  
  176.     result = (first << 16) + second;
  177. #ifdef    CRAY
  178.     if (result & 0x80000000)
  179.         result = result - 0x100000000;
  180. #endif
  181.     return(result);
  182. }
  183.  
  184.  
  185. void
  186. Write32Bits(FILE *fp, int i)
  187. {
  188.     Write16BitsLowHigh(fp,(int)(i&0xffffL));
  189.     Write16BitsLowHigh(fp,(int)((i>>16)&0xffffL));
  190. }
  191.  
  192.  
  193. void
  194. Write32BitsLowHigh(FILE *fp, int i)
  195. {
  196.     Write16BitsLowHigh(fp,(int)(i&0xffffL));
  197.     Write16BitsLowHigh(fp,(int)((i>>16)&0xffffL));
  198. }
  199.  
  200.  
  201. void
  202. Write32BitsHighLow(FILE *fp, int i)
  203. {
  204.     Write16BitsHighLow(fp,(int)((i>>16)&0xffffL));
  205.     Write16BitsHighLow(fp,(int)(i&0xffffL));
  206. }
  207.  
  208. void ReadBytes(FILE *fp, char *p, int n)
  209. {
  210.     while ((!feof(fp) & n--) > 0)
  211.         *p++ = getc(fp);
  212. }
  213.  
  214. void ReadBytesSwapped(FILE *fp, char *p, int n)
  215. {
  216.     char    *q = p;
  217.  
  218.     while ((!feof(fp) & n--) > 0)
  219.         *q++ = getc(fp);
  220.  
  221.     for (q--; p < q; p++, q--){
  222.         n = *p;
  223.         *p = *q;
  224.         *q = n;
  225.     }
  226. }
  227.  
  228. void WriteBytes(FILE *fp, char *p, int n)
  229. {
  230.     while (n-- > 0)
  231.         putc(*p++, fp);
  232. }
  233.  
  234. void WriteBytesSwapped(FILE *fp, char *p, int n)
  235. {
  236.     p += n-1;
  237.     while (n-- > 0)
  238.         putc(*p--, fp);
  239. }
  240.  
  241. #if 0
  242.  
  243. defdouble
  244. ReadIeeeFloatHighLow(FILE *fp)
  245. {
  246.     char    bits[kFloatLength];
  247.  
  248.     ReadBytes(fp, bits, kFloatLength);
  249.     return ConvertFromIeeeSingle(bits);
  250. }
  251.  
  252. defdouble
  253. ReadIeeeFloatLowHigh(FILE *fp)
  254. {
  255.     char    bits[kFloatLength];
  256.  
  257.     ReadBytesSwapped(fp, bits, kFloatLength);
  258.     return ConvertFromIeeeSingle(bits);
  259. }
  260.  
  261. defdouble
  262. ReadIeeeDoubleHighLow(FILE *fp)
  263. {
  264.     char    bits[kDoubleLength];
  265.  
  266.     ReadBytes(fp, bits, kDoubleLength);
  267.     return ConvertFromIeeeDouble(bits);
  268. }
  269.  
  270. defdouble
  271. ReadIeeeDoubleLowHigh(FILE *fp)
  272. {
  273.     char    bits[kDoubleLength];
  274.  
  275.     ReadBytesSwapped(fp, bits, kDoubleLength);
  276.     return ConvertFromIeeeDouble(bits);
  277. }
  278.  
  279. #endif /* #if 0*/
  280.  
  281. defdouble
  282. ReadIeeeExtendedHighLow(FILE *fp)
  283. {
  284.     char    bits[kExtendedLength];
  285.  
  286.     ReadBytes(fp, bits, kExtendedLength);
  287.     return ConvertFromIeeeExtended(bits);
  288. }
  289.  
  290. #if 0
  291.  
  292. defdouble
  293. ReadIeeeExtendedLowHigh(FILE *fp)
  294. {
  295.     char    bits[kExtendedLength];
  296.  
  297.     ReadBytesSwapped(fp, bits, kExtendedLength);
  298.     return ConvertFromIeeeExtended(bits);
  299. }
  300.  
  301. void
  302. WriteIeeeFloatLowHigh(FILE *fp, defdouble num)
  303. {
  304.     char    bits[kFloatLength];
  305.  
  306.     ConvertToIeeeSingle(num,bits);
  307.     WriteBytesSwapped(fp,bits,kFloatLength);
  308. }
  309.  
  310. void
  311. WriteIeeeFloatHighLow(FILE *fp, defdouble num)
  312. {
  313.     char    bits[kFloatLength];
  314.  
  315.     ConvertToIeeeSingle(num,bits);
  316.     WriteBytes(fp,bits,kFloatLength);
  317. }
  318.  
  319. void
  320. WriteIeeeDoubleLowHigh(FILE *fp, defdouble num)
  321. {
  322.     char    bits[kDoubleLength];
  323.  
  324.     ConvertToIeeeDouble(num,bits);
  325.     WriteBytesSwapped(fp,bits,kDoubleLength);
  326. }
  327.  
  328. void
  329. WriteIeeeDoubleHighLow(FILE *fp, defdouble num)
  330. {
  331.     char    bits[kDoubleLength];
  332.  
  333.     ConvertToIeeeDouble(num,bits);
  334.     WriteBytes(fp,bits,kDoubleLength);
  335. }
  336.  
  337. #endif /* #if 0*/
  338.  
  339. void
  340. WriteIeeeExtendedLowHigh(FILE *fp, defdouble num)
  341. {
  342.     char    bits[kExtendedLength];
  343.  
  344.     ConvertToIeeeExtended(num,bits);
  345.     WriteBytesSwapped(fp,bits,kExtendedLength);
  346. }
  347.  
  348.  
  349. void
  350. WriteIeeeExtendedHighLow(FILE *fp, defdouble num)
  351. {
  352.     char    bits[kExtendedLength];
  353.  
  354.     ConvertToIeeeExtended(num,bits);
  355.     WriteBytes(fp,bits,kExtendedLength);
  356. }
  357.